package com.zengge.zbc.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServiceException extends RuntimeException {

    private final static Logger logger = LoggerFactory.getLogger(ServiceException.class);

    private int code;
    private String message;
    private ErrorCode errorCode;
    public ServiceException(ErrorCode code) {
        this.code = code.code;
        this.message = code.message;
        this.errorCode = code;
    }


    public static ServiceException toError(ErrorCode code){
        return new ServiceException(code);
    }

    public static ServiceException toException(Throwable throwable) {
        if(throwable instanceof ServiceException){
            return (ServiceException) throwable;
        }
        logger.error(throwable.getMessage());
        return new ServiceException(ErrorCode.SERVICE_ERROR);
    }

    public int getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public ErrorCode getErrorCode() {
        return errorCode;
    }
}
